package xyz.naokeziteng.sort;

/**
 * @author hawk
 * @date 2022/5/5 19:44
 * @desc 线性查找
 **/
public class LinearSearch {

    private LinearSearch() {
    }

    /**
     * 线性查找
     *
     * @return
     */
    public static <T> int linerSearch(T[] data, T target) {
        for (int i = 0; i < data.length; i++) {
            if (target.equals(data[i])) {
                return i;
            }
        }

        return -1;
    }


    public static void main(String[] args) {
        int n = 1000000;
        Integer[] data = ArrayGenerator.generateOrderedArray(n);
        long start = System.nanoTime();
        for (int k = 0; k < 100; k++) {
            LinearSearch.linerSearch(data, n);
        }
        long end = System.nanoTime();
        double time = (end - start) / 1000000000.0;
        System.out.println("执行时间(100次): " + time + "s");

    }
}
